package com.boot.study;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author likz
 * @Date 2024/12/18 17:13
 * @Description
 **/
public class WordContent {

    @Test
    public void textExcel(){
        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\likz\\Desktop\\健帆\\CTCAE50.xlsx");
        List<Map<String, Object>> readAll = reader.readAll();
        System.out.println(readAll);
    }

    public static void main(String[] args) {
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\likz\\Desktop\\健帆\\CTCAE500.docx"));


            int pageNumber = 1;
            boolean newPage = true;
            // 读取分页
//            for (XWPFParagraph paragraph : document.getParagraphs()) {
//                String text = paragraph.getText();
//                if (text != null && text.contains("\u000B")) { // '\u000B' 是Word中分页的特殊字符
//                    newPage = true;
//                    continue;
//                }
//                XWPFDocument document1 = paragraph.getDocument();
//            }

            // 读取表格
            for (XWPFTable table : document.getTables()) {
                for (int row = 0; row < table.getNumberOfRows(); row++) {
                    System.out.println("====第"+row+"行开始====\n");
                    for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) {
                        XWPFTableCell cell = table.getRow(row).getCell(col);
                        CTDecimalNumber gridSpan = cell.getCTTc().getTcPr().getGridSpan();
                        // 获取跨列数
                        // 获取跨列数
                        int colSpan = 1;
                        if (cell.getCTTc().getTcPr().isSetGridSpan()) {
                            colSpan = cell.getCTTc().getTcPr().getGridSpan().getVal().intValue();
                        }
                        // 获取跨行数
                        int rowSpan = 1;
                        if (cell.getCTTc().getTcPr().isSetVMerge()) {
                            CTVMerge vMerge = cell.getCTTc().getTcPr().getVMerge();
                            STMerge.Enum val1 = vMerge.getVal();
                            if (vMerge.isSetVal()) {
                                STMerge.Enum val = vMerge.getVal();
                                if (val == STMerge.RESTART) {
                                    // 查找后续行中与当前单元格合并的行数
                                    for (int r = row + 1; r < table.getNumberOfRows(); r++) {
                                        XWPFTableRow nextRow = table.getRow(r);
                                        if (nextRow.getCell(col).getCTTc().getTcPr().isSetVMerge()) {
                                            CTVMerge nextVMerge = nextRow.getCell(col).getCTTc().getTcPr().getVMerge();
                                            if (nextVMerge.getVal() == STMerge.CONTINUE) {
                                                rowSpan++;
                                            } else {
                                                break;
                                            }
                                        } else {
                                            break;
                                        }
                                    }
                                }
                            }


                        }

                        CTTc ctTc = cell.getCTTc();
                        CTTcPr tcPr = ctTc.getTcPr();
                        String cellText = cell.getText();
                        System.out.print("单元格(" + row + "," + col + "): 文本=" + cellText + ", 列合并=" + colSpan + ", 行合并=" + rowSpan + "\n");
                    }
                    System.out.println("\n====第"+row+"行结束====");
                }

            }

            document.close();


        }catch (Exception e){

        }

    }
    @Test
    public  void testPage() {
        String filePath = "C:\\Users\\likz\\Desktop\\健帆\\CTCAE51.docx"; // 建议使用配置文件管理此路径

        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            int pageNumber = 1;
            boolean newPage = true;

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                String text = paragraph.getText();
                if (text != null && text.contains("\u000B")) { // '\u000B' 是Word中分页的特殊字符
                    System.out.println("分页符位于段落: " + text);
                    newPage = true;
                    continue;
                }

                if (newPage) {
                    System.out.println("第 " + pageNumber + " 页的表格数据:");
                    newPage = false;
                    pageNumber++;
                }

                for (XWPFTable table : document.getTables()) {
                    for (int row = 0; row < table.getNumberOfRows(); row++) {
                        System.out.println("====第"+row+"行开始====\n");
                        for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) {
                            XWPFTableCell cell = table.getRow(row).getCell(col);
                            String cellText = cell.getText();
                            System.out.print(cellText + "\t");
                        }
                        System.out.println("\n====第"+row+"行结束====");
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test1(){

        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\likz\\Desktop\\健帆\\CTCAE500.docx"));

            // 读取表格
            for (XWPFTable table : document.getTables()) {

                for (int row = 0; row < table.getNumberOfRows(); row++) {
                    XWPFTableRow tableRow = table.getRow(row);
                    CTRow ctRow = tableRow.getCtRow();
                    List<XWPFTableCell> tableCells = tableRow.getTableCells();

                    System.out.println("====第"+row+"行开始====\n");
                    for (int col = 0; col < tableCells.size(); col++) {
                        XWPFTableCell cell = table.getRow(row).getCell(col);
                         // 列索引从0开始
                            String cellText = cell.getText();
                            System.out.print(cellText + "");

                    }
                    System.out.println("\n====第"+row+"行结束====");
                }

            }

            document.close();


        }catch (Exception e){

        }





    }







}
